package com.jifeng.api.crud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.api.crud.dao.DeviceinfoDao;
import com.jifeng.api.crud.entity.DeviceinfoEntity;
import com.jifeng.api.crud.service.DeviceinfoService;
import com.jifeng.api.utils.*;
import com.jifeng.api.utils.PageUtils;
import com.jifeng.api.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;
import java.util.Objects;


@Service("deviceinfoService")
@Slf4j
public class DeviceinfoServiceImpl extends ServiceImpl<DeviceinfoDao, DeviceinfoEntity> implements DeviceinfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DeviceinfoEntity> page = this.page(
                new Query<DeviceinfoEntity>().getPage(params),
                new QueryWrapper<DeviceinfoEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public R add(DeviceinfoEntity deviceinfo) {
        try {
            boolean save = this.save(deviceinfo);
            if (save) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("path：{}，errMsg：{}", "增加设备信息", e.getMessage());
            return R.sysError();
        }

    }

    @Override
    public R myUpdate(DeviceinfoEntity deviceinfo) {
        try {
            boolean update = this.updateById(deviceinfo);
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("path：{}，errMsg：{}", getRequestUrl(), e.getMessage());
            return R.sysError();
        }

    }
    public String getRequestUrl(){
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest().getRequestURI();
    }
}